#include<bits/stdc++.h>
using namespace std;
#define rd(i,n) for(int i=0;i<n;i++)
#define rp(i,n) for(int i=1;i<=n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef unsigned long long ull;
const int N=135005;
int n,m,k,t,X[4],ori[N],a[N],c[N];
int dt[N<<1],ban[N<<1],dep[N<<1],mxid[N<<1],currt,cur[N<<1];
ll sum[N<<1],dp[N<<1],res,ans[N];
inline void allban(int i){
    if(ban[i])return;
    ban[i]=1;
    if(dep[i]==0){
        if(sum[i]){
            res-=sum[i];
        }else if(cur[i]){
            res-=dp[i];
        }
        return;
    }
    allban(i<<1);
    allban(i<<1|1);
}
inline void check(int i){
    if(!sum[i])return;
    int ls=(i<<1)+dt[i];
    int rs=(i<<1)+(!dt[i]);
    if(!sum[ls]&&a[dp[ls]-1]>=dep[i]){
        if(!ban[rs]){
            allban(rs);
        }
        sum[i]=0,dp[i]=dp[ls];
        if(i!=currt)check(i>>1);
    }else if(!sum[ls]&&!sum[rs]){
        sum[i]=0,dp[i]=dp[rs];
        if(i!=currt)check(i>>1);
    }
}
inline void update(int i,int v){
    if(ban[i])return;
    res-=sum[i];
    sum[i]=0,dp[i]=v;
    if(a[v-1]>=mxid[i])cur[i]=1,res+=v;
    if(i!=currt)check(i>>1);
}
inline void solve(){
    rd(i,(1<<k))sum[i+(1<<k)]=i+1;
    per(i,1,(1<<k)-1){
        sum[i]=sum[i<<1]+sum[i<<1|1];
    }
    rep(i,1,(1<<(k+1))-1){
        dp[i]=sum[i];
        ban[i]=0;
    }
    currt=(1<<k),res=1;
    int sz=0;
    rd(i,n){
        if(i==1||(i!=0&&__lg(i)!=__lg(i-1))){
            currt>>=1;
            if(!dt[currt]){
                if(a[dp[currt<<1]-1]>=dep[currt]){
                    dp[currt]=res=dp[currt<<1]+sum[currt<<1|1];
                    allban(currt<<1|1);
                    check(currt);
                }else{
                    dp[currt]=res=sum[currt<<1|1];
                    cur[dp[currt<<1]-1+(1<<k)]=0;
                }
            }else{
                dp[currt]=res=sum[currt<<1|1]+dp[currt<<1];
            }
            sz++;
        }
        update(i+(1<<k),i+1);
        ans[i+1]=res;
    }
    ll totans=0;
    rp(i,m)totans^=(i*ans[c[i]]);
    cout<<totans<<'\n';
}
inline void build(int i,int d){
    dep[i]=d,mxid[i]=mxid[i>>1];
    if(__builtin_popcount(i)==1)mxid[i]=0;
    else if((i&1)==dt[i>>1]){
        mxid[i]=max(mxid[i],dep[i>>1]);
    }
    if(d==0)return;
    build(i<<1,d-1);
    build(i<<1|1,d-1);
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    k=__lg(n-1)+1;
    rd(i,n)cin>>ori[i];
    rp(i,m)cin>>c[i];
    per(d,1,k){
        string s;
        cin>>s;
        rep(i,0,(1<<(d-1))-1){
            dt[i+(1<<(d-1))]=s[i]-'0';
        }
    }
    build(1,k);
    cin>>t;
    rd(_,t){
        rd(i,4)cin>>X[i];
        rd(i,n)a[i]=ori[i]^(X[(i+1)%4]);
        solve();
    }
    return 0;
}
//g++ arena.cpp -o arena -O2 -std=c++14 -static
